{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-02T13:09:08.664314Z",
     "start_time": "2025-06-02T13:09:08.523477Z"
    }
   },
   "source": [
    "import pymysql\n",
    "class Select:\n",
    "    def open_mysql(self):\n",
    "        try:\n",
    "            self.connect = pymysql.connect(\n",
    "                host='localhost',\n",
    "                port=3306,\n",
    "                user='root',\n",
    "                password='123456',\n",
    "                database='dodan_movie',  # 确保数据库名正确\n",
    "                charset='utf8mb4'\n",
    "            )\n",
    "            print('连接成功')\n",
    "        except Exception as e:\n",
    "            print(\"连接异常\",e)\n",
    "    def close_mysql(self):\n",
    "       if self.connect is not None:\n",
    "           self.connect.close()\n",
    "\n",
    "    def get_best_movie(self):\n",
    "        self.open_mysql()\n",
    "        try:\n",
    "            with self.cursor() as cursor:\n",
    "                sql=\"select count(*) from dodan_movie\"\n",
    "                cursor.execute(sql)\n",
    "                result = cursor.fetchone()\n",
    "                return result\n",
    "        except Exception as e:\n",
    "            print(\"查询失败\",e)\n",
    "        finally:\n",
    "            self.close_mysql()\n",
    "    def get_best_first_actor(self):\n",
    "        self.open_mysql()\n",
    "        try:\n",
    "            with self.cursor() as cursor:\n",
    "                sql=\"select first_actor from dodan_movie\"\n",
    "                cursor.execute(sql)\n",
    "                result = cursor.fetchone()\n",
    "                return result\n",
    "        except Exception as e:\n",
    "            print(\"查询失败\",e)\n",
    "        finally:\n",
    "            self.close_mysql()\n",
    "    def get_most_comments_movie(self):\n",
    "        self.open_mysql()\n",
    "        try:\n",
    "            with self.cursor() as cursor:\n",
    "                sql=\"select movie_name from dodan_movie order by commment desc limit 10\"\n",
    "                cursor.execute(sql)\n",
    "                result = cursor.fetchone()\n",
    "                return result\n",
    "        except Exception as e:\n",
    "            print(\"查询失败\",e)\n",
    "        finally:\n",
    "            self.close_mysql()\n",
    "result=Select().get_most_comments_movie()\n",
    "print(result)\n",
    "\n",
    "\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "连接成功\n",
      "查询失败 'check' object has no attribute 'cursor'\n",
      "None\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-02T14:16:22.806003Z",
     "start_time": "2025-06-02T14:16:22.774470Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pymysql\n",
    "\n",
    "class Check:\n",
    "    def __init__(self):\n",
    "        self.connect = None\n",
    "\n",
    "    def open_mysql(self):\n",
    "        try:\n",
    "            self.connect = pymysql.connect(\n",
    "                host='localhost',\n",
    "                port=3306,\n",
    "                user='root',\n",
    "                password='123456',\n",
    "                database='dodan_movie',\n",
    "                charset='utf8mb4'\n",
    "            )\n",
    "            print('连接成功')\n",
    "            return True\n",
    "        except Exception as e:\n",
    "            print(\"连接异常\", e)\n",
    "            return False\n",
    "\n",
    "    def close_mysql(self):\n",
    "        if self.connect is not None:\n",
    "            self.connect.close()\n",
    "\n",
    "    def get_most_comments_movie(self):\n",
    "        if not self.open_mysql():\n",
    "            return None\n",
    "        try:\n",
    "            cursor = self.connect.cursor()\n",
    "            sql = \"SELECT movie_name FROM dodan_movie ORDER BY comments DESC LIMIT 10\"\n",
    "            cursor.execute(sql)\n",
    "            result = cursor.fetchall()\n",
    "            return result\n",
    "        except Exception as e:\n",
    "            print(\"查询失败\", e)\n",
    "        finally:\n",
    "            self.close_mysql()\n",
    "        return None\n",
    "\n",
    "# 创建类的实例并调用方法\n",
    "check = Check()\n",
    "result = check.get_most_comments_movie()\n",
    "print(result)"
   ],
   "id": "dd073defdf7bd674",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "连接成功\n",
      "(('肖申克的救赎',), ('这个杀手不太冷',), ('千与千寻',), ('泰坦尼克号',), ('阿甘正传',), ('霸王别姬',), ('我不是药神',), ('盗梦空间',), ('疯狂动物城',), ('星际穿越',))\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "e102fb642606e6b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
